<?php
namespace app\crm\model;
use think\Model;
use think\Db;
use think\Cache;
use think\Loader;

class Cdr extends Model
{
	/**
     * 获取回访任务列表
     * @return array <回访任务列表信息>
     */
    public function getCdrData()
    {
		$page = $_REQUEST["page"];
		$limit = $_REQUEST["limit"];
		$where = " 1 ";
        $where .= empty($_REQUEST["src"])?"":" AND src = '".$_REQUEST["src"]."'";
        $where .= empty($_REQUEST["dst"])?"":" AND dst = '".$_REQUEST["dst"]."'";
        $where .= empty($_REQUEST["workno"])?"":" AND workno = '".$_REQUEST["workno"]."'";
        $where .= empty($_REQUEST["calltype"])?"":" AND calltype = '".$_REQUEST["calltype"]."'";
        $where .= empty($_REQUEST["disposition"])?"":" AND disposition = '".$_REQUEST["disposition"]."'";
        $where .= empty($_REQUEST["lastapp"])?"":" AND lastapp = '".$_REQUEST["lastapp"]."'";
        
        $dept_id = $_REQUEST['dept_id'];
        $d_id = $_SESSION['user_info']['d_id'];
        $arrDep = Loader::model('Reportmanager')->getDepTreeData();
        $deptst = Loader::model('Reportmanager')->getMeAndSubDeptID($arrDep,$dept_id);  //取上级部门
        $deptSet = rtrim($deptst,",");
        
        $deptst2 = Loader::model('Reportmanager')->getMeAndSubDeptID($arrDep,$d_id);  //取上级部门
        $deptSet2 = rtrim($deptst2,",");
        
        if(!$dept_id){
            $deptId = $deptSet2;
        }else{
            $deptId = $deptSet;
        }

        // print_r($_REQUEST);

        // if($username != "admin" || $dept_id){
        //     $where .= " AND dept_id in ($deptId)";
        // }



        $count = Db::table('asteriskcdrdb.cdr')->where($where)->count();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $dataList = Db::table('asteriskcdrdb.cdr')->where($where)->limit(($page-1)*$limit,$limit)->order("calldate desc")->select();
		
		$cdr_status_row = array('ANSWERED'=>'已接听','NO ANSWER'=>'未接听');
		//$cdr_calltype = array('IN'=>$in_answer,'OUT'=>$out_answer);
		$cdr_calltype = array('IN'=>"呼入",'OUT'=>"呼出","AUTOCALL"=>"自动外呼呼出","PREVIEW"=>"预览式外呼","PREDICTION"=>"预测式外呼","LOCAL"=>"分机互打");
		$cdr_calltype2 = array('IN'=>"呼入",'OUT'=>"呼出","AUTOCALL"=>"自动外呼呼出","PREVIEW"=>"预览式外呼","PREDICTION"=>"预测式外呼","LOCAL"=>"分机互打");
		$lastapp_row = array("Dial"=>"坐席","Queue"=>"队列","Playback"=>"语音","Busy"=>"忙","Congestion"=>"忙","Hangup"=>"挂机");
		
		$users = Cache::get('users');
		foreach($dataList as &$val){
			$val["lastapp2"] = $val["lastapp"];
			$val["lastapp"] = $lastapp_row[$val["lastapp"]];
			if(!$val["lastapp"]){
				$val["lastapp"] = $val["lastapp2"];
			}
			
			if($val["dst"] == "toqueue"){
				$val["src"] = trim(array_shift(explode("\" <",$val["clid"])),"\"");
				$val["dst"] = array_pop(explode("/",array_shift(explode("@",$val["dstchannel"]))));
			}
			
			$val["srcP"] = trim($val["src"]);
			$val["dstP"] = trim($val["dst"]);
			if($val["calltype"] == "IN"){
				$val["call_phone"] = $val["srcP"];
			}else{
				$val["call_phone"] = $val["dstP"];
			}
			
			$status = $cdr_status_row[$val["disposition"]];
			$val["disposition"] = $status;
			$Incalltype = $cdr_calltype[$val["calltype"]];
			$val["calltype"] = $Incalltype;
			
			$arrBill[] = $val["billsec"];
			
			$val['username'] = $users[$val['workno']];
			//$val['dept_name'] = $deptId_name[$val['dept_id']];
			$val["uniqueid"] = trim($val["uniqueid"]);
			//$pbxCallData[$i]['operations'] = "";
			$arrTmp = explode('.',$val["uniqueid"]);
			$timestamp = $arrTmp[0];
			$dirPath = '/var/spool/asterisk/monitor/' . date('Y-m',$timestamp) .'/'. date('d',$timestamp);
			$WAVfile = $dirPath ."/".$val["uniqueid"].".WAV";
			$val["WAVfile"] = $WAVfile;
		}
        $rowsList = count($dataList) ? $dataList : false;
        $arrCate["code"] = 0;
        $arrCate["msg"] = '';
        $arrCate["count"] = $count;
        $arrCate["data"] = $rowsList;
        return $arrCate;
    }


    public function recordEvaluation ($data)
    {
        $result = Db::name('outbound_record_evaluate')->insert($data);
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'评分成功！'));
        } else {
            echo json_encode(array('msg'=>'评分失败！'));
        }
    }


    public function getCdrInfo ($uniqueid)
    {
        $dataList = Db::table('asteriskcdrdb.cdr')->where('uniqueid="'.$uniqueid.'"')->find();
        return $dataList;
    }


}

?>

